package cn.unknowpupil.init.common.service;

import cn.unknowpupil.init.properties.FileProperties;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.File;
import java.util.List;

/**
 * Created at 2020/12/15 by w10g <br>
 * Initialize InitializeService ... <br>
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Service
@Slf4j
public class InitializeService {
    @Autowired
    private FileProperties fileProperties;

    /**
     * java中File类自带一个检测方法exists可以判断文件或文件夹是否存在，一般与mkdirs方法（该方法相较于mkdir可以创建包括父级路径，推荐使用该方法）或者createNewFile方法合作使用。
     *
     * @throws Exception
     */
    @PostConstruct
    public void init() throws Exception {
        log.info("Initializing Source Directory...");
        String avatar = fileProperties.getPath().getAvatar();
        String path = fileProperties.getPath().getPath();
        String resource = fileProperties.getPath().getResource();
        List<String> list = Lists.newArrayList();
        list.add(avatar);
        list.add(path);
        list.add(resource);
        list.forEach(o -> {
            File file = new File(o);
            if (!file.exists()) {
                //创建目录
                file.mkdirs();
                log.info("Initializing {} Directory...",o);
            }
        });
//            File file2 = new File("D:\\xxx\\sss\\xx.txt");
//            if(!file2 .exists()) {
//                //创建文件
//                file2.createNewFile();
//                System.out.println("测试文件不存在");
//            }
    }
}
